package com.nd.android.musicboxlibrary.ui.common;

import java.io.File;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.common.android.utils.FileUtils;
import com.google.gson.Gson;
import com.nd.android.musicboxlibrary.business.bean.FileInfo;
import com.nd.android.musicboxlibrary.business.bean.MusicInfo;
import com.nd.android.musicboxlibrary.interf.ManagerFactory;
import com.nd.android.musicboxlibrary.util.Log;
import com.ngds.library.downloadcenter.AppDownloadHelper;
import com.ngds.library.downloadcenter.AppDownloadHelperImpl;
import com.product.android.utils.LogUtils;

/**
 * DownloadSuccessReceiver Description: Author:lix Date :13-12-25
 */
public class DownloadSuccessReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction();
		if (AppDownloadHelper.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
			doOwnBiz(context, intent);
		}
	}
 
	/**
	 * <br>
	 * Created 2014年9月11日 下午11:52:35
	 *
	 * @param context Context
	 * @param intent Intent
	 * @author wat.ztag
	 */
	protected void doOwnBiz(Context context, Intent intent) {
		try {
			long downloadId = getDownloadId(intent);
			String url = intent.getStringExtra(AppDownloadHelper.EXTRA_DOWNLOAD_URI);
			String FileName = intent.getStringExtra(AppDownloadHelper.EXTRA_FILE_NAME);
			LogUtils.d("DownloadSuccessReceiver.java -- jiangwb", "downloadId:"+downloadId+" url:"+url + " FileName:" + FileName);
			AppDownloadHelper appDownloadHelperImpl = AppDownloadHelperImpl.getInstance(context);
			if (!TextUtils.isEmpty(appDownloadHelperImpl.getFileName(downloadId))) {
				// 下载成功，更新数据库: 音乐， Lrc
				String description = appDownloadHelperImpl.getDescription(context, url);
				FileInfo fileInfo = new Gson().fromJson(description, FileInfo.class);
				if (fileInfo != null && (fileInfo.getType() == FileInfo.TYPE_MUSIC 
						|| fileInfo.getType() == FileInfo.TYPE_HQ_MUSIC)) {
					//by jiangwb
					MusicInfo musicInfo = new MusicInfo();
					musicInfo.setId(fileInfo.getId());
					musicInfo.setHQId(fileInfo.getHQId());
					musicInfo.setMusicName(fileInfo.getName());
					String path = "";
					if(fileInfo.getType() == FileInfo.TYPE_HQ_MUSIC){//by jiangwb
						musicInfo.setHQ(true);
						path = musicInfo.getHQMusicTemp();
					}else{
						musicInfo.setHQ(false);
						path = musicInfo.getMusicTemp();
					}
					musicInfo.setDownLoading(false);
					musicInfo.setDownLoaded(true);
					ManagerFactory.getDownloadInstance(context).setMusicDownloaded(musicInfo);
					ManagerFactory.getPlayerInstance().initMusicList(context);
					if(!TextUtils.isEmpty(path)){ 
						File file = new File(path); 
						if(!TextUtils.isEmpty(FileName) && !FileName.toLowerCase().equals(path.toLowerCase())){
							File downloadFile = new File(FileName);		
							if(file.exists() && downloadFile.length() > 0){
								file.delete();
								file = downloadFile;
								LogUtils.d("DownloadSuccessReceiver.java -- jiangwb", "downloadId:"+downloadId+" delete 多余的temp文件");
							}
						}
						
						LogUtils.d("DownloadSuccessReceiver.java -- jiangwb", "downloadId:"+downloadId+" new File(path)");
						if(file.exists() && file.length() > 0){
							LogUtils.d("DownloadSuccessReceiver.java -- jiangwb", "downloadId:"+downloadId+" FileUtils.copyFile");
							if(musicInfo.isHQ()){
								//file.renameTo(new File(musicInfo.getHQMusicPath())); 
								File localFile = new File(musicInfo.getHQMusicPath());
								if(localFile.exists() && localFile.length() > 0 && localFile.length() == file.length()){
									//本地已经存在了，就不需要覆盖了
								}else{
									FileUtils.copyFile(file, new File(musicInfo.getHQMusicPath()));									
								}
								file.delete();
							}else{
								//file.renameTo(new File(musicInfo.getMusicPath()));  
								File localFile = new File(musicInfo.getMusicPath());
								if(localFile.exists() && localFile.length() > 0 && localFile.length() == file.length()){
									//本地已经存在了，就不需要覆盖了
								}else{
									FileUtils.copyFile(file, new File(musicInfo.getMusicPath()));
								}
								file.delete();
							}
						}else{//如果发现temp文件大小为0，说明是自己new file出来的，需要删除掉
							file.delete();
						}
					}
				}
			}
		} catch (Exception e) {
			Log.d("DownloadSuccessReceiver Global");
		}
	}

	/**
	 * <br>
	 * Created 2014年9月11日 下午11:52:38
	 *
	 * @param intent Intent
	 * @return long
	 * @author wat.ztag
	 */
	public long getDownloadId(Intent intent) {
		return intent.getLongExtra(AppDownloadHelper.EXTRA_DOWNLOAD_ID, 0);
	}
}
